<?php
include_once("member_class.php");
include_once("bonus_class.php");
include_once("system_class.php");
include_once("ulevel_class.php");
include_once("../alisms/sendSms.php");

class member_class{

    function jinbwang($uid,$username,$pid,$nickname){
        $bonus=new bonus_class();
        $sql="select * from `bmember` where (chl=0 or chr=0 or chn=0)  order by id asc limit 0,1";
        $row=getOne($sql);
        if($row){
                $sum=pow(3,$row['plevel']);
                $cengsum1=$this->getcengsum($row['plevel'],1);
                $cengsum2=$this->getcengsum($row['plevel'],2);
                $cengsum3=$this->getcengsum($row['plevel'],3);
                if($cengsum1<$sum){
                    $sqlc="select * from `bmember` where chl=0   order by id asc limit 0,1";
                }elseif($cengsum2<$sum){
                    $sqlc="select * from `bmember` where  chr=0   order by id asc limit 0,1";
                }elseif ($cengsum3<$sum){
                    $sqlc="select * from `bmember` where  chn=0  order by id asc limit 0,1";
                }
                $us=getOne($sqlc);
                $member_update=NULL;
                if($us['chl']==0){
                    $treeplace=1;
                    edit_sql("update `bmember` set chl=".$uid." where id=".$us['id']." ");
                }elseif($us['chr']==0){
                    $treeplace=2;
                    edit_sql("update `bmember` set chr=".$uid." where id=".$us['id']." ");
                }elseif($us['chn']==0){
                    $treeplace=3;
                    edit_sql("update `bmember` set chn=".$uid." where id=".$us['id']." ");
                }
            $iscengsum=$this->getceng($row['plevel'],$treeplace);
            if($iscengsum){
                $cengsum_update['sum']=$iscengsum['sum']+1;
                edit_update_cl('cengpeng',$cengsum_update,$iscengsum['id']);
            }else{
                $cengpeng_update['ceng']=$row['plevel'];
                $cengpeng_update['treeplace']=$treeplace;
                $cengpeng_update['sum']=1;
                add_insert_cl("cengpeng",$cengpeng_update);
            }
                $member_update['treeplace']=$treeplace;
                $member_update['userid']=$uid;
                $member_update['username']=$username."_".$pid;
                $member_update['nickname']=$nickname."_".$pid;
                $member_update['fatherid']=$us['id'];
                $member_update['fathername']=$us['nickname'];
                $member_update['plevel']=$us['plevel']+1;
                $member_update['ppath']="".$us['ppath'].$us['id'].",";
                $member_update['pdt']=date("Y-m-d H:i:s");
                $member_update['ispay']=1;
                add_insert_cl("bmember",$member_update);
                $usernamenew="{$nickname}_{$pid}";
                $nickname=getBMemberbyNickName($usernamenew);
/*                print_r($nickname);*/
                $bonus->b2bonusB($nickname['id'],$nickname['nickname'],$nickname['ppath'],$nickname['plevel']);
                $bonus->b0bonus();
        }else{
            $treeplace=0;
            $member_update['treeplace']=$treeplace;
            $member_update['userid']=$uid;
            $member_update['nickname']=$nickname."_".$pid;
            $member_update['username']=$username."_".$pid;
            $member_update['fatherid']=1;
            $member_update['fathername']=1;
            $member_update['plevel']=0;
            $member_update['ppath']=",";
            $member_update['pdt']=date("Y-m-d H:i:s");
            $member_update['ispay']=1;
            add_insert_cl("bmember",$member_update);
        }
    }
    function jinbwang2($uid,$username,$pid,$nickname){
        $bonus=new bonus_class();
      // $sql="select * from `bmember` where (chl=0 or chr=0 )  order by id asc limit 0,1";
        $sql="select * from `bmember` where (chl=0 or chr=0) and ispay>0 order by plevel,pdt asc limit 0,1";
        $row=getOne($sql);
        if($row){
                $member_update=NULL;
                if($row['chl']==0){
                    $treeplace=1;
                    edit_sql("update `bmember` set chl=".$uid." where id=".$row['id']." ");
                }elseif($row['chr']==0){
                    $treeplace=2;
                    edit_sql("update `bmember` set chr=".$uid." where id=".$row['id']." ");
                }
                $member_update['treeplace']=$treeplace;
                $member_update['userid']=$uid;
                $member_update['username']=$username."_".$pid;
                $member_update['nickname']=$nickname."_".$pid;
                $member_update['fatherid']=$row['id'];
                $member_update['fathername']=$row['nickname'];
                $member_update['plevel']=$row['plevel']+1;
                $member_update['ppath']="".$row['ppath'].$row['id'].",";
                $member_update['pdt']=date("Y-m-d H:i:s");
                $member_update['ispay']=1;
                $member_update['ulevel']=1;
                add_insert_cl("bmember",$member_update);
                edit_sql("update member set pid=pid+1 where id=".$uid);
                $usernamenew="{$nickname}_{$pid}";
                $nickname=getBMemberbyNickName($usernamenew);
                $bonus->b2bonus3($nickname['id'],$nickname['nickname'],$nickname['ppath'],$nickname['plevel']);
                $bonus->b0bonus0();
        }else{
            $treeplace=0;
            $member_update['treeplace']=$treeplace;
            $member_update['userid']=$uid;
            $member_update['nickname']=$nickname."_".$pid;
            $member_update['username']=$username."_".$pid;
            $member_update['fatherid']=1;
            $member_update['fathername']=1;
            $member_update['ulevel']=1;
            $member_update['plevel']=0;
            $member_update['ppath']=",";
            $member_update['pdt']=date("Y-m-d H:i:s");
            $member_update['ispay']=1;
            add_insert_cl("bmember",$member_update);
            edit_sql("update member set pid=pid+1 where id=".$uid);
        }
    }
    //根据treeplace、层数、获取当前层有多少个
    function  getcengsum($ceng,$treeplace){
        $sql="select *  from cengpeng where ceng=".$ceng." and treeplace=".$treeplace." ";
        $num=getOne($sql);
        return $num['sum'];
    }
    function  getceng($ceng,$treeplace){
        $sql="select *  from cengpeng where ceng=".$ceng." and treeplace=".$treeplace." ";
        $num=getOne($sql);
        return $num;
    }




	//排小区
function xiaogongpai($uid,$reid){

    $reus=$this->getMemberbyID($reid);

    if($reus['plevel2']>0){
        $reusplevel2=$reus['plevel2'];
    }else{
        $reusplevel2=$reus['plevel'];
    }
    $istreeplace=$this->istreeplace($reus['plevel2'],1,$reid);
    if($istreeplace==1){
        $sql="select * from `member` where plevel=".$reusplevel2." and (ppath like '%,".$reid.",%' or id=".$reid.") and chl=0  and ispay>0 order by pid asc limit 0,1";
    }else{
        $sql="select * from `member` where plevel=".$reusplevel2." and (ppath like '%,".$reid.",%' or id=".$reid.") and  chr=0 and ispay>0 order by pid desc limit 0,1";
    }
	if($query = mysql_query($sql)){
		while ($row=mysql_fetch_array($query)){
			$member_update=NULL;
			if($istreeplace==1){
				$treeplace=1;
				edit_sql("update `member` set chl=".$uid." where id=".$row['id']." ");
			}elseif($istreeplace==2){
				$treeplace=2;
				edit_sql("update `member` set chr=".$uid." where id=".$row['id']." ");
			}

				$member_update['treeplace']=$istreeplace;
				$member_update['fatherid']=$row['id'];
				$member_update['fathername']=$row['nickname'];
				$member_update['plevel']=$row['plevel']+1;
				$member_update['ppath']="".$row['ppath'].$row['id'].",";
				if($istreeplace==1){
				    $pid=$row['pid']*2-1;
                }else{
				    $pid=$row['pid']*2;
                }
				$member_update['pid']=$pid;
				edit_update_cl("member",$member_update,$uid);
				$this->isman($uid);
		}
	}
}

    function sanxiaogongpai($uid,$reid){
        $sql="select * from `member` where (ppath like '%,".$reid.",%' or id=".$reid.") and (chl=0 or chr=0 or chn=0) and ispay>0 order by id asc limit 0,1";
        if($query = mysql_query($sql)){
            while ($row=mysql_fetch_array($query)){
                $member_update=NULL;
                if($row['chl']==0){
                    $treeplace=1;
                    edit_sql("update `member` set chl=".$uid." where id=".$row['id']."");
                }elseif($row['chr']==0){
                    $treeplace=2;
                    edit_sql("update `member` set chr=".$uid." where id=".$row['id']."");
                }elseif($row['chn']==0){
                    $treeplace=3;
                    edit_sql("update `member` set chn=".$uid." where id=".$row['id']."");
                }
                $member_update['treeplace']=$treeplace;
                $member_update['fatherid']=$row['id'];
                $member_update['fathername']=$row['nickname'];
                $member_update['plevel']=$row['plevel']+1;
                $member_update['ppath']="".$row['ppath'].$row['id'].",";
                edit_update_cl("member",$member_update,$uid);
            }
        }
    }



function  pangwang($id){
    $us=$this->getMemberbyID($id);
    $reus=$this->getMemberbyID($us['reid']);
    $admin=$this->getMemberbyID(1);
    if($reus['plevel2']>$admin['plevel2']||$reus['plevel2']==0&&$admin['plevel2']<$reus['plevel']){
        $istreeplace=$this->istreeplace($admin['plevel2'],1,$admin['id']);
        if($istreeplace==1){
            $sql="select * from member where plevel=".$admin['plevel2']." and chl=0 and ispay>0 order by pid asc limit 0,1";
        }else{
            $sql="select * from member where plevel=".$admin['plevel2']." and chr=0 and ispay>0 order by pid  desc limit 0,1";
        }
        $row=getOne($sql);
        if($istreeplace==1){
            edit_sql("update `member` set chl=".$id." where id=".$row['id']." ");
        }elseif($istreeplace==2){
            edit_sql("update `member` set chr=".$id." where id=".$row['id']." ");
        }
        $member_update['treeplace']=$istreeplace;
        $member_update['fatherid']=$row['id'];
        $member_update['fathername']=$row['nickname'];
        $member_update['plevel']=$row['plevel']+1;
        $member_update['ppath']="".$row['ppath'].$row['id'].",";
        if($istreeplace==1){
            $pid=$row['pid']*2-1;
        }else{
            $pid=$row['pid']*2;
        }
        $member_update['pid']=$pid;
        edit_update_cl("member",$member_update,$id);
        $this->isman($id);
    }else{
        $this->xiaogongpai($id,$us['reid']);
    }
}

function  istreeplace($plevel3,$treeplace,$reid){
    $reus=$this->getMemberbyID($reid);
    if($plevel3>0){
        $cha=$plevel3+1-$reus['plevel'];
        $sql="select count(*) as ss from member where plevel=".($plevel3+1)." and treeplace=".$treeplace." and (ppath like '%,".$reid.",%') ";
        $zong=pow(2,$cha)/2;
    }else{
        $sql="select count(*) as ss from member where plevel=".($reus['plevel']+1)." and treeplace=".$treeplace." and (ppath like '%,".$reid.",%')";
        $cha=$plevel3+1-$reus['plevel'];
        $zong=pow(2,$cha)/2;
    }
    $ss=getOne($sql);
    if($zong>$ss['ss']){
        return 1;
    }else{
        return 2;
    }
}
function  isman($id){
    $us=$this->getMemberbyID($id);
    $sql="select * from `member` where id in(0".$us['ppath']."0) and ".$us['plevel']."-plevel<=".$us['plevel']." ";
    if($query = mysql_query($sql)){
        while ($row=mysql_fetch_array($query)){
            $cha=$us['plevel']-$row['plevel'];
            $zrs=pow(2,$cha);
            $sql="select count(*) as ss from member where plevel=".$us['plevel']." and (ppath like '%,".$row['id'].",%') and ispay>0";
            $ss=getOne($sql);
            if($zrs==$ss['ss']){
                edit_sql("update member set plevel2=".$us['plevel']." where id=".$row['id']);
            }
        }
    }
}


    function paiqu($uid){
        $_bonus_cl=new bonus_class();
        $us=$this->getMemberbyID($uid);
        $qid=$this->paiwangfanhuiid($us['reid']);
        $reus=getMemberbyID($qid);
        $treeplace=1;
        edit_sql("update `member` set chl=".$uid." where id=".$reus['id']."");
                $member_update['treeplace']=$treeplace;
                $member_update['fatherid']=$reus['id'];
                $member_update['fathername']=$reus['nickname'];
                $member_update['plevel']=$reus['plevel']+1;
                $member_update['ppath']="".$reus['ppath'].$reus['id'].",";
                edit_update_cl("member",$member_update,$uid);

    }

//检查会员编号是否存在
function checkUserID($UserID){
  
}
//检查会员编号是否存在
function checkNickName($NickName){
	$sql = "SELECT * FROM `member` WHERE nickname='".$NickName."'";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//检查会员编号是否存在并激活
function checkNickNameispay($NickName){
	$sql = "SELECT * FROM `member` WHERE nickname='".$NickName."' and isPay>0";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//会员登录验证
function checkLogin($NickName,$PassWord){
	$sql="select * from `Member` where nickname='".$NickName."' AND password1='".$PassWord."'";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//会员二级密码验证
function checkPassword2($NickName,$PassWord2){
	$sql="select * from `Member` where nickname='".$NickName."' AND password2='".$PassWord2."'";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//会员三级密码验证
function checkPassword3($NickName,$PassWord3){
	$sql="select * from `Member` where nickname='".$NickName."' AND password3='".$PassWord3."'";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//密码问题验证
function checkQuestion($NickName,$passanswer){
	$sql="select * from `Member` where nickname='".$NickName."' AND passanswer='".$passanswer."'";
	$query=mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;	
	}
}
//根据ID获取会员信息
function getMemberbyID($_id){
	$sql = "SELECT * FROM `member` WHERE id=".$_id;
	$query=mysql_query($sql);
	return mysql_fetch_array($query);
}

//根据昵称获取会员信息
function getMemberbyUserId($UserId){
	$sql = "SELECT * FROM `member` where userid='".$UserId."'";
	$query=mysql_query($sql);
	return mysql_fetch_array($query);
}
//根据昵称获取会员信息
function getMemberbyNickName($NickName){
	$sql = "SELECT * FROM `member` where nickname='".$NickName."'";
	$query=mysql_query($sql);
	return mysql_fetch_array($query);
}
//检查该位置是否有人
function checkFatherMan($FatherID,$TreePlace){
	$sql = "SELECT * FROM `member` WHERE fatherid=".$FatherID." AND treeplace=".$TreePlace."";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;
	}
}
//获得安置位置的人的信息
function getFatherManbyFidAndTreeplace($FatherID,$TreePlace){
	$sql = "SELECT * FROM `member` WHERE fatherid=".$FatherID." AND treeplace=".$TreePlace."";
	$query = mysql_query($sql);
	return mysql_fetch_array($query);
}

//查询该编号推荐的人的集合
function getMemberListByreid($reid){
	$sql = "SELECT * FROM `member` WHERE reid=".$reid."";
	$query = mysql_query($sql);
	return mysql_fetch_array($query);
}
/*判断id的团队中是否有uid
*return true在团队中,false不在
*/
function checkisppath($id,$uid){
	$re=getMemberbyID($uid);
	return ereg(",".$id.",",$re['ppath']);
}
/*判断id的团队中是否有nickname
*return true在团队中,false不在
*/
function checkisrepath($id,$nickname){
	$re=getMemberbyNickName($nickname);
	return ereg(",".$id.",",$re['repath']);
}

//判断是否有新邮件
function checknewmail($_id){
	$sql = "SELECT * FROM `mail` WHERE isread=0 and sid=".$_id;
	$query = mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return true;
	}else{
		return false;
	}
}

function checkfman($_id){
	$sql = "SELECT * FROM `member` WHERE fatherid=".$_id."";
	$query = mysql_query($sql);
	if(mysql_num_rows($query) >= 1){
		return false;
	}else{
		return true;
	}	
}

function getMaxPid(){
	$sql="select id from `member` where pcount<10 and ispay>0 order by pdt,id limit 0,1";
	$query = mysql_query($sql);
	$pid=mysql_fetch_array($query);
	return $pid[0];
}

//写入激活记录
function addbdrecord($us,$bd,$lsk,$sygwb,$syzsq){
	$bdrecord=NULL;
	$bdrecord['uid']=$us['id'];
	$bdrecord['nickname']=$us['nickname'];
	$bdrecord['lsk']=$lsk;
	$bdrecord['bdid']=$bd['id'];
	$bdrecord['bdname']=$bd['nickname'];
	$bdrecord['bddate']=now();
	$bdrecord['zsq']=$syzsq;
	$bdrecord['gwb']=$sygwb;
	add_insert_cl('bdrecord',$bdrecord);
}

function Bwang(){//B网大公排
    $_ulevel=new ulevel_class();
    $ulevel_cl=$_ulevel->getulevelbyulevel(1);
	$sql="select * from `member` where maxmey >= {$ulevel_cl['yl10']} and (area1+area2)>{$ulevel_cl['yl11']} and isb2=0";

	if($query = mysql_query($sql)){
		while ($row=mysql_fetch_array($query)){
			 $sql2="select * from `member` where (chl2=0 or chr2=0) and isb2=1 order by plevel2,pdt2 limit 0,1";
				if($query2 = mysql_query($sql2)){
					if(mysql_num_rows($query2)>=1){
						while ($row2=mysql_fetch_array($query2)){
							if($row2['chl2']==0){
								$treeplace2=1;
								edit_sql("update `member` set chl2=".$row['id']." where id=".$row2['id']." ");
							}elseif($row2['chr2']==0){
								$treeplace2=2;
								edit_sql("update `member` set chr2=".$row['id']." where id=".$row2['id']." ");
							}
							$member_update['treeplace2']=$treeplace2;//int(11)
							$member_update['fatherid2']=$row2['id'];//int(11)
							$member_update['fathername2']=$row2['nickname'];//varchar(30)
							$member_update['plevel2']=$row2['plevel2']+1;//int(11)
							$member_update['ppath2']="".$row2['ppath2'].$row2['id'].",";//text
							$member_update['isb2']=1;//int(11)
							$member_update['pdt2']=now();//datetime
							edit_update_cl("member",$member_update,$row['id']);
							file_put_contents("D://addB.txt",date('y-m-d h:m:s'.$row['id'].'/t'),FILE_APPEND);
						}
					}else {
						$member_update['isb2']=$row['isb2']+1;//int(11)
						edit_update_cl("member",$member_update,$row['id']);
					}
				}
		}
      //  $this->jiangli();
	}
}
//进C网奖励
function jiangli(){
    $bonus=new bonus_class();
    $_ul=new ulevel_class();
    $ulevel=$_ul->getulevelbyulevel(1);
    $sql="select * from member where isb2>0";
    $query=mysql_query($sql);
    while ($row=mysql_fetch_assoc($query)){
        $plevel2=$row['plevel2']+$ulevel['yl12'];
        $sql="select * from member where plevel2=".$plevel2;
        $num=getAll($sql);
        $sum=count($num);
        $renshu=pow(2,$plevel2);
        if($sum==$renshu){
            $member_update['gwb']=$row['gwb']+$ulevel['yl13']/2;
            $member_update['mey']=$row['mey']+$ulevel['yl13']/2;
            $member_update['maxmey']=$row['maxmey']+$ulevel['yl13']/2;
            edit_update_cl("member",$member_update,$row['id']);
            $beizhu1=$ulevel['yl13']."C网奖励积分";
            $bonus->bonus_laiyuan($row['id'],$row['nickname'],$row['id'],$row['nickname'],11,$ulevel['yl13'],$beizhu1);

        }
    }
}




function addbwang($reid){
	$rs=getMemberbyID($reid);
	$_bonus_cl=new bonus_class();
	$sql="select * from `member` where ulevel>=2 and isb=0";
	if($query = mysql_query($sql)){
		while ($row=mysql_fetch_array($query)){
			if ($rs['fatherid2'] or $rs['id']==1){
			$sql2="select * from `member` where (ppath like '%,".$reid.",%' or id=".$reid.") and (chl2=0 or chr2=0) and isb=1 order by plevel2,pdt2 limit 0,1";
			}else {
				$sql2="select * from `member` where (chl2=0 or chr2=0) and isb=1 order by plevel2,pdt2 limit 0,1";		
			}
			if($query2 = mysql_query($sql2)){
				while ($row2=mysql_fetch_array($query2)){
					if($row2['chl2']==0){
						$treeplace2=1;
						edit_sql("update `member` set chl2=".$row['id']." where id=".$row2['id']."");
					}elseif($row2['chr2']==0){
						$treeplace2=2;
						edit_sql("update `member` set chr2=".$row['id']." where id=".$row2['id']."");	
					}
					$member_update['treeplace2']=$treeplace2;
					$member_update['fatherid2']=$row2['id'];
					$member_update['fathername2']=$row2['nickname'];
					$member_update['plevel2']=$row2['plevel2']+1;
					$member_update['ppath2']="".$row2['ppath2'].$row2['id'].",";
//					$member_update['cfxf']=$row['cfxf']-1000;
					$member_update['isb']=1;
					$member_update['pdt2']=now();
					edit_update_cl("member",$member_update,$row['id']);
					$_bonus_cl->b2bonus($row['id'], $row['nickname'], $row[lsk], $row[ulevel]);
					$_bonus_cl->b2bonuss($row['id'], $row['nickname'], $row[lsk], $row[ulevel]);
					$_bonus_cl->b2bonussj($row['id'], $row['nickname'], $row[lsk], $row[ulevel]);
				}
			}
		}
	}
}


function dagongpai($uid){
    $us=getBMemberbyID($uid);
    $bonus=new bonus_class();
	$sql="select * from `bmember` where (chl=0 or chr=0) and ispay>0 order by plevel,pdt asc limit 0,1";
	if($query = mysql_query($sql)){
		while ($row=mysql_fetch_array($query)){
		    if(!empty($row)){
                $member_update=NULL;
                if($row['chl']==0){
                    $treeplace=1;
                    $sql="update `bmember` set chl=".$us['id']." where id=".$row['id']."";
                    edit_sql($sql);
                }elseif($row['chr']==0){
                    $treeplace=2;
                    $sql="update `bmember` set chr=".$us['id']." where id=".$row['id']."";
                    edit_sql($sql);
                }
                $member_update['ispay']=1;
                $member_update['treeplace']=$treeplace;
                $member_update['fatherid']=$row['id'];
                $member_update['fathername']=$row['nickname'];
                $member_update['plevel']=$row['plevel']+1;
                $member_update['ppath']="".$row['ppath'].$row['id'].",";
                edit_update_cl("bmember",$member_update,$us['id']);
            }else{
                $member_update=NULL;
                $member_update['ispay']=1;
                $member_update['userid']=$uid;
                $member_update['treeplace']=0;
                $member_update['fatherid']=0;
                $member_update['fathername']=0;
                $member_update['plevel']=0;
                $member_update['ppath']=",0,";
                add_insert_cl("bmember",$member_update);
            }

		}
		$bus=getBMemberbyID($us['id']);
        $bonus->b2bonus3($bus['id'],$bus['nickname'],$bus['ppath'],$bus['plevel']);
        $bonus->b0bonus0();
	}
}


function paiwangfanhuiid($id){
    $sql="select * from member where treeplace=1 and fatherid=".$id." and ispay>0";
    $query=mysql_query($sql);
    $row=mysql_fetch_assoc($query);
    if(!empty($row)){
      return $this->paiwangfanhuiid($row['id']);
    }else{
      return $id;
    }
}
//激活会员
//激活会员
    function jihuomember($id,$type){
        $_bonus_cl=new bonus_class();
        $_ulevel_cl=new ulevel_class();
        $us=getMemberbyID($id);
        $us_update['ispay']=1;
        $us_update['gwb']=$us['lsk'];
        $us_update['pdt']=now();
        $reman=getMemberbyID($us['reid']);
        $_systemyeji=new system_class();
        $reman_update['recount']=$reman['recount']+1;
        $reman_update['reyeji']=$reman['reyeji']+$us['dan'];
        edit_update_cl('member',$us_update,$us['id']);
        edit_update_cl('member',$reman_update,$reman['id']);
    //    $this->sanxiaogongpai($us['id'],$us['reid']);
        $this->addArea($us['id'],$us['treeplace'],$us['lsk']);
        $_systemyeji->yejitongji(1,$us['dan'],$us['lsk'],0,0,0,0);
        $_sys=$_systemyeji->system_information(1);
        $_update_system['yeji']=$_sys['yeji']+$us['lsk'];
        $_systemyeji->system_update($_update_system);

       // $_bonus_cl->tuijian($us['id'],$us['lsk']);//直推奖
        //直推奖
        $_bonus_cl->b1bouns($us['id']);
        //层碰奖
	    $_bonus_cl->cengpeng($us['id']);
        //对碰奖
     //     $_bonus_cl->b3bonusd();

        //业绩奖
      //  $_bonus_cl->b4bonus($id);
        //见点奖
        $_bonus_cl->b2bonus3($us['id'],$us['nickname'],$us['ppath'],$us['plevel']);
        if($type==2){
            $_bonus_cl->baodan($us['id']);
        }


        //见点奖
      //  $newus=$this->getMemberbyID($us['id']);
      //  $_bonus_cl->b2bonus2($us['id'],$us['nickname'],$us['ppath'],$us['plevel']);

        $_bonus_cl->b0bonus();
    //    $this->isfuli();

        $send = new alisendsms();
        //通知 管理员 新会员加入
        $send->sendSms($_sys['access_key_id'],$_sys['access_key_secret'],$_sys['sms_admin_tel'],'SMS_135800037','name',$us['userid']);
        //通知 会员 缴费成功
        $send->sendSms($_sys['access_key_id'],$_sys['access_key_secret'],$us['userid'],'SMS_135795052','name',$us['username']);
    }

    function isfuli(){
        $ulevel=new ulevel_class();
        $sql="select area1,area2,id,ulevel from member where area1>0 and area2>0";
        $query=mysql_query($sql);
        while ($row=mysql_fetch_assoc($query)){
            $_iul=$ulevel->getulevelbyulevel($row['ulevel']);
            $area=$row['area1']>$row['area2']?$row['area2']:$row['area1'];
            if($area>=$_iul['yl8']){
                edit_sql("update member set ulevel2=5,fathername2='五星'  where id=".$row['id'] );
            }elseif($area>=$_iul['yl7']){
                edit_sql("update member set ulevel2=4,fathername2='四星'  where id=".$row['id'] );
            }elseif($area>=$_iul['yl6']){
                edit_sql("update member set ulevel2=3,fathername2='三星'  where id=".$row['id'] );
            }elseif($area>=$_iul['yl5']){
                edit_sql("update member set ulevel2=2,fathername2='二星'  where id=".$row['id'] );
            }elseif($area>=$_iul['yl4']){
                edit_sql("update member set ulevel2=1,fathername2='一星'  where id=".$row['id'] );
            }
        }
    }



    function addbwangtiaojian(){
        $sql="select * from member where wlf>=200 and ispay>0";
        $query=mysql_query($sql);
        while ($row=mysql_fetch_assoc($query)){
            $this->jinbwangtiaojian($row['id']);
        }
    }
    function jinbwangtiaojian($id){
        $member=new member_class();
        $us=getMemberbyID($id);
        if($us['wlf']>=200){
            $member->jinbwang2($us['id'],$us['username'],($us['pid']+1),$us['nickname']);
            edit_sql("update member set wlf=wlf-200 where id=".$id);
        }
    }
function newanzhi($treeplace,$id,$reid){
    if($treeplace==1){
        $sql="select * from member where fatherid=".$reid." and treeplace=1";
        $zuoqu=getOne($sql);
        if(empty($zuoqu)){
            $user_id=$reid;
        }else{
            $user_id=$this->get_tree_place($zuoqu['id'],1,$zuoqu['id']);
        }

        $sql="select * from member where id=".$user_id;
        $father=getOne($sql);
        $map['fatherid'] = $father['id'];  //安置人id
        $map['plevel'] = $father['plevel']+1;  //安置人层数
        $map['ppath'] = $father['ppath'].$father['id'].",";  //安置关系id
        $map['fathername'] = $father['userid'];  //安置人id
    }else{
        $sql="select * from member where fatherid=".$reid." and treeplace=2";
        $youqu=getOne($sql);
        if(empty($youqu)){
            $user_id=$reid;
        }else{
            $user_id=$this->get_tree_place($youqu['id'],1,$youqu['id']);
            $treeplace=1;
        }
        $sql="select * from member where id=".$user_id;
        $father=getOne($sql);
        $map['fatherid'] = $father['id'];  //安置人id
        $map['fathername'] = $father['userid'];  //安置人id
        $map['plevel'] = $father['plevel']+1;  //安置人层数
        $map['ppath'] = $father['ppath'].$father['id'].",";  //安置关系id
    }
    $map['treeplace'] = $treeplace;
    edit_update_cl("member",$map,$id);
}
function get_tree_place($father_id,$tree_plece,$user_id){
    $sql="select id,fatherid,treeplace from member where fatherid=".$father_id." and treeplace=".$tree_plece." ";
    $user=getOne($sql);
        if($user){
            return  $this->get_tree_place($user['id'],1,$user['id']);
        }else{
            return $user_id;
        }
    }

    //判断回填单是否满足正式会员
/*function  ismember(){
    $sql="select * from member where ispay=3";
    $query=mysql_query($sql);
    while ($row=mysql_fetch_assoc($query)){
        if($row['lsk']<=$row['mey']){
            edit_sql("update member set ispay=1,mey=mey-{$row['lsk']} where id=".$row['id']);
        }
    }
}*/
function  anzhi($id,$table){
    $us=getMemberbyID($id);
    if($table==1){
        $sql="select * from member where ispay>0 order by  pid desc limit 0,1";
        $array=getOne($sql);
        $member_update['FatherID']=$array['id'];
        $member_update['plevel']=$array['plevel']+1;
        $member_update['ppath']="".$array['ppath'].$array['id'].",";
        $member_update['area1']=$array['area1'];
        $member_update['TreePlace']=1;
        $member_update['futou']=1;
        $member_update['pid']=$array['pid']+1;
        edit_update_cl('member',$member_update,$id);
    }else{
        $sql="select * from member2 where ispay>0 order by  pid desc limit 0,1";
        $array=getOne($sql);
        if(empty($array)){
            $member_update['FatherID']=0;
            $member_update['plevel']=0;
            $member_update['ppath']="0,";
            $member_update['area1']=0;
            $member_update['TreePlace']=1;
            $member_update['futou']=2;
            $member_update['pid']=0;
            $member_update['userid']=$us['id'];
            $member_update['ispay']=1;
            $member_update['nickname']=$us['nickname'];
            $member_update['ulevel']=$us['ulevel'];
            add_insert_cl('member2',$member_update);
        }else{
            $member_update['FatherID']=$array['id'];
            $member_update['plevel']=$array['plevel']+1;
            $member_update['ppath']="".$array['ppath'].$array['id'].",";
            $member_update['area1']=$array['area1'];
            $member_update['TreePlace']=1;
            $member_update['futou']=2;
            $member_update['pid']=$array['pid']+1;
            $member_update['userid']=$us['id'];
            $member_update['ispay']=1;
            $member_update['nickname']=$us['nickname'];
            $member_update['ulevel']=$us['ulevel'];
            add_insert_cl('member2',$member_update);
        }

    }

}

function jihuomember1($id){
	$_bonus_cl=new bonus_class();
	$us=getMemberbyID($id);
	$us_update['ispay']=1;
	$us_update['mianfei']=1;
	//	$us_update['sgb']=$us['lsk'];
	$us_update['pdt']=now();
	if ($us['ulevel']==2){
		// 		$us_update['gu']=1;
		edit_sql ("update `member` set  gu=gu+1 where id=1");
	}elseif ($us['ulevel']==3){
		// 		$us_update['gu']=11;
		edit_sql ("update `member` set  gu=gu+11 where id=1");
	}
	$reman=getMemberbyID($us['reid']);
	$reman_update['recount']=$reman['recount']+1;
	$reman_update['reyeji']=$reman['reyeji']+$us['dan'];
	edit_update_cl('member',$us_update,$us['id']);
	edit_update_cl('member',$reman_update,$reman['id']);
	// 	$this->addArea($us['id'], $us['treeplace'], $us['lsk']);

	$_systemyeji=new system_class();
	$_systemyeji->yejitongji(1,$us['dan'],$us['lsk'],0,0,0,0);

	$_sys=$_systemyeji->system_information(1);
	$_update_system['yeji']=$_sys['yeji']+$us['lsk'];
	$_systemyeji->system_update($_update_system);

	if ($us['ulevel']>=1){
		$this->xiaogongpai($us['id'], $us[reid],$us['lsk']);
	}
	$_bonus_cl->b0bonus($us['id'], $us[nickname]);
// 	$_bonus_cl->b1bonus($us['id'], $us[nickname], $us['reid'],$us['ulevel']);

}
//顶层会员
function dingcengmember($id){
	$us=getMemberbyID($id);
	$us_update['ispay']=1;
	//$us['gwb']=$us['lsk'];
	$us_update['pdt']=now();
	$us_update['fatherid']=0;
	$us_update['fathername']="顶层会员";
	$us_update['plevel']=0;
	$us_update['ppath']=",";
	$us_update['treeplace']=1;
	$pid=$this->getMaxPid();
	$us_update['pid']=$pid;
	edit_sql("update `member` set pcount=pcount+1 where id=".$pid."");
	$reman=getMemberbyID($us['reid']);
	$reman_update['recount']=$reman['recount']+1;
	$reman_update['reyeji']=$reman['reyeji']+$us['dan'];
	edit_update_cl('member',$us_update,$us['id']);
	edit_update_cl('member',$reman_update,$us['reid']);
	$_systemyeji=new system_class();
	$_systemyeji->yejitongji(1,$us['dan'],$us['lsk'],0,0,0,0);
}

//空单会员
function kongdanmember($id){
	$us=getMemberbyID($id);
	$us_update['ispay']=2;
	$us_update['pdt']=now();
	$pid=$this->getMaxPid();
	$us_update['pid']=$pid;
	edit_sql("update `member` set pcount=pcount+1 where id=".$pid."");
	$reman=getMemberbyID($us['reid']);
	$reman_update['recount']=$reman['recount']+1;
	edit_update_cl('member',$us_update,$us['id']);
	edit_update_cl('member',$reman_update,$us['reid']);
	//$this->addArea($us['id'],$us['treeplace'],$us['dan']);
	$_systemyeji=new system_class();
	$_systemyeji->yejitongji(1,0,0,0,0,0,0);
}
//回填会员
function huitiandanmember($id){
    $_ulevel_cl=new ulevel_class();
	$us=getMemberbyID($id);
	$us_update['ispay']=3;
	$us_update['pdt']=now();
/*	$pid=$this->getMaxPid();*/
	$us_update['pid']=1;
    $_iul=$_ulevel_cl->getulevelbyulevel($us['ulevel']);

	$reman=getMemberbyID($us['reid']);
	$reman_update['recount']=$reman['recount']+1;
	edit_update_cl('member',$us_update,$us['id']);
	edit_update_cl('member',$reman_update,$us['reid']);
	//$this->addArea($us['id'],$us['treeplace'],$us['dan']);
	$_systemyeji=new system_class();
	$_systemyeji->yejitongji(1,0,0,0,0,0,0);
}

/*
*修改推荐人
*/
function update_reman($_upreman){
	$upreman=getMemberbyNickName($_upreman);
	$sql="select * from `member` where rname='".$_upreman."'";
	if($query = mysql_query($sql)){
		while($row=mysql_fetch_array($query)){
			$up_member['reid']=$upreman['id'];
			$up_member['rname']=$upreman['nickname'];
			$up_member['repath']=$upreman['repath'].$upreman['id'].",";
			$up_member['relevel']=$upreman['relevel']+1;
			edit_update_cl('member',$up_member,$row['id']);
			$this->update_reman($row['nickname']);
		}
	}
}

/*
给上级新增业绩
* $id给上级新增业绩的会员
* $treeplace 区域
* $dan 业绩
*/
function addArea($id,$treeplace,$dan){
    $_systemyeji=new system_class();
    $_sys=$_systemyeji->system_information(1);
	if ($us=getMemberbyID($id)){
		if ($fman=getMemberbyID($us['fatherid'])){
			switch($us['treeplace']){
				case 0:
					$fman_update['area1']=$fman['area1']+$dan; #总业绩
					$fman_update['yarea1']=$fman['yarea1']+$dan; #结余业绩
					$fman_update['narea1']=$fman['narea1']+1; #总人数
					break;
				case 1:
					$fman_update['area1']=$fman['area1']+$dan; #总业绩
                    $fman_update['yarea1']=$fman['yarea1']+$dan; #结余业绩
					$fman_update['narea1']=$fman['narea1']+1; #总人数

					break;
				case 2:
					$fman_update['area2']=$fman['area2']+$dan; #总业绩
                    $fman_update['yarea2']=$fman['yarea2']+$dan; #结余业绩
					$fman_update['narea2']=$fman['narea2']+1; #总人数
		
					break;
				case 3:
					$fman_update['area3']=$fman['area3']+$dan; #总业绩
					$fman_update['yarea3']=$fman['yarea3']+$dan; #结余业绩
					$fman_update['narea3']=$fman['narea3']+1; #总人数

					break;
				case 4:
					$fman_update['area4']=$fman['area4']+$dan; #总业绩
					$fman_update['yarea4']=$fman['yarea4']+$dan; #结余业绩
					$fman_update['narea4']=$fman['narea4']+1; #总人数

					break;
				case 5:
					$fman_update['area5']=$fman['area5']+$dan; #总业绩
					$fman_update['yarea5']=$fman['yarea5']+$dan; #结余业绩
					$fman_update['narea5']=$fman['narea5']+1; #总人数

					break;
				case 6:
					$fman_update['area6']=$fman['area6']+$dan; #总业绩
					$fman_update['yarea6']=$fman['yarea6']+$dan; #结余业绩
					$fman_update['narea6']=$fman['narea6']+1; #总人数
	
					break;
				case 7:
					$fman_update['area7']=$fman['area7']+$dan; #总业绩
					$fman_update['yarea7']=$fman['yarea7']+$dan; #结余业绩
					$fman_update['narea7']=$fman['narea7']+1; #总人数

					break;
			}
			edit_update_cl('member',$fman_update,$fman['id']);
			$this->addArea($fman['id'],$fman['treeplace'],$dan);
		}
	}
}/*
给上级新增业绩
* $id给上级新增业绩的会员
* $treeplace 区域
* $dan 业绩
*/
function addArea2($id,$treeplace,$dan){
    $_systemyeji=new system_class();
    $_sys=$_systemyeji->system_information(1);
	if ($us=getMemberbyID($id)){
		if ($fman=getMemberbyID($us['fatherid'])){
			switch($us['treeplace']){
				case 0:
					$fman_update['area1']=$fman['area1']+$dan; #总业绩
					$fman_update['yarea1']=$fman['yarea1']+$dan; #结余业绩
					$fman_update['narea1']=$fman['narea1']+1; #总人数
					break;
				case 1:
				//	$fman_update['area1']=$fman['area1']+$dan; #总业绩
                    $fman_update['yarea1']=$fman['yarea1']+$dan; #结余业绩
				//	$fman_update['narea1']=$fman['narea1']+1; #总人数

					break;
				case 2:
			//		$fman_update['area2']=$fman['area2']+$dan; #总业绩
                    $fman_update['yarea2']=$fman['yarea2']+$dan; #结余业绩
			//		$fman_update['narea2']=$fman['narea2']+1; #总人数

					break;
				case 3:
					$fman_update['area3']=$fman['area3']+$dan; #总业绩
					$fman_update['yarea3']=$fman['yarea3']+$dan; #结余业绩
					$fman_update['narea3']=$fman['narea3']+1; #总人数

					break;
				case 4:
					$fman_update['area4']=$fman['area4']+$dan; #总业绩
					$fman_update['yarea4']=$fman['yarea4']+$dan; #结余业绩
					$fman_update['narea4']=$fman['narea4']+1; #总人数

					break;
				case 5:
					$fman_update['area5']=$fman['area5']+$dan; #总业绩
					$fman_update['yarea5']=$fman['yarea5']+$dan; #结余业绩
					$fman_update['narea5']=$fman['narea5']+1; #总人数

					break;
				case 6:
					$fman_update['area6']=$fman['area6']+$dan; #总业绩
					$fman_update['yarea6']=$fman['yarea6']+$dan; #结余业绩
					$fman_update['narea6']=$fman['narea6']+1; #总人数

					break;
				case 7:
					$fman_update['area7']=$fman['area7']+$dan; #总业绩
					$fman_update['yarea7']=$fman['yarea7']+$dan; #结余业绩
					$fman_update['narea7']=$fman['narea7']+1; #总人数

					break;
			}
			edit_update_cl('member',$fman_update,$fman['id']);
			$this->addArea2($fman['id'],$fman['treeplace'],$dan);
		}
	}
}

}
?>